﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;

namespace FastUtil.Log
{
    public class LogUtil
    {
        //设置读写锁为写入模式独占资源，其他写入请求需要等待本次写入结束之后才能继续写入
        //注意：长时间持有读线程锁或写线程锁会使其他线程发生饥饿 (starve)。 为了得到最好的性能，需要考虑重新构造应用程序以将写访问的持续时间减少到最小。
        //从性能方面考虑，请求进入写入模式应该紧跟文件操作之前，在此处进入写入模式仅是为了降低代码复杂度
        //因进入与退出写入模式应在同一个try finally语句块内，所以在请求进入写入模式之前不能触发异常，否则释放次数大于请求次数将会触发异常
        static ReaderWriterLockSlim LogWriteLock = new ReaderWriterLockSlim();
        /// <summary>
        /// 写入日志，默认根目录的Log文件夹，文件名为log_yyyyMMdd
        /// </summary>
        /// <param name="LogContent">日志内容</param>
        public static void WriteLog(string LogContent)
        {
            try
            {
                LogWriteLock.EnterWriteLock();
                string path = AppDomain.CurrentDomain.BaseDirectory;
                path = System.IO.Path.Combine(path
                , "Logs\\");
                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }
                string fileFullName = System.IO.Path.Combine(path, string.Format("{0}.txt", "log_" + DateTime.Now.ToString("yyyyMMdd")));
                using (StreamWriter output = System.IO.File.AppendText(fileFullName))
                {
                    output.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + LogContent);

                    output.Close();
                }
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                LogWriteLock.ExitWriteLock();
            }

        }

        /// <summary>
        ///  写入日志，默认根目录的Log文件夹
        /// </summary>
        /// <param name="FileName">日志名</param>
        /// <param name="LogContent">日志内容</param>
        public static void WriteLog(string FileName, string LogContent)
        {
            try
            {
                LogWriteLock.EnterWriteLock();
                string path = AppDomain.CurrentDomain.BaseDirectory;
                path = System.IO.Path.Combine(path
                , "Logs\\");
                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }
                string fileFullName = System.IO.Path.Combine(path, string.Format("{0}.txt", FileName + "_" + DateTime.Now.ToString("yyyyMMdd")));
                using (StreamWriter output = System.IO.File.AppendText(fileFullName))
                {
                    output.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + LogContent);

                    output.Close();
                }
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                LogWriteLock.ExitWriteLock();
            }

        }

        /// <summary>
        ///  写入日志
        /// </summary>
        /// <param name="DirPath">日志所属文件夹名称，在根目录创建</param>
        /// <param name="FileName">日志名</param>
        /// <param name="LogContent">日志内容</param>
        public static void WriteLog(string DirPath, string FileName, string LogContent, bool InCloud = false)
        {
            try
            {
                LogWriteLock.EnterWriteLock();
                string path = string.Empty;
                if (InCloud)
                {
                    path = AppDomain.CurrentDomain.BaseDirectory;
                    path = System.IO.Path.Combine(path
                    , DirPath + "\\");
                }
                else
                {
                    path = DirPath;
                }
                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }
                string fileFullName = System.IO.Path.Combine(path, string.Format("{0}.txt", FileName + "_" + DateTime.Now.ToString("yyyyMMdd")));
                using (StreamWriter output = System.IO.File.AppendText(fileFullName))
                {
                    output.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + LogContent);

                    output.Close();
                }
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                LogWriteLock.ExitWriteLock();
            }
        }
    }
}
